/*
 * Copyright 2009 (c) Patrick Pietens
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package indigo.display 
{
	import indigo.loader.events.LoadEvent;
	import indigo.loader.loadSingle;
	import indigo.loader.loaders.ImageLoader;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.events.Event;
	import flash.geom.Rectangle;
	/**
	 * The <code>ImageContainer</code> is a <code>DisposableDisplayObject</code>
	 * which acts as a container to add images. Images are loaded automatically
	 * when a filename is provided.
	 * 
  	 * @example
	 * <listing version="3.0">
	 * </listing>
	 * 
   	 * @playerversion Flash 9
	 * @langversion ActionScript 3.0
	 * 
	 */
	public class ImageContainer extends ExtendedMovieClip 
	{
		/**
		 * Scales the image to the user defined width and height
		 */
		public static const MODE_SCALE:String = "scale";
		/**
		 * Crops the image to the user defined width and height
		 */
		public static const MODE_CROP:String = "crop";

		
		/**
		 * Number that indicates the height of the ImageContainer 
		 */
		private var _height:Number = NaN;
		/**
		 * Bitmap that represents the attached image of the ImageContainer
		 */
		private var _image:Bitmap;
		/**
		 * Integer that indicates the depth of the Bitmap
		 */
		private var _index:Number = Number.MAX_VALUE;
		/**
		 * Reference to the ImageLoader which is used to load the image
		 */
		private var _loader:ImageLoader;
		/**
		 * String that indicates the resize mode that is used by the ImageContainer
		 */
		private var _resizeMode:String = MODE_SCALE;
		/**
		 * Number that indicates the horizontal scaling of the ImageContainer
		 */
		private var _scaleX:Number = 1.0;
		/**
		 * Number that indicates the vertical scaling of the ImageContainer
		 */
		private var _scaleY:Number = 1.0;
		/**
		 * Number that indicates the width of the Image
		 */
		private var _width:Number = NaN;

		
		/**
		 * Constructor
		 * @param String, Class, BitmapData or Bitmap thats represent the image
		 * 	
		 */
		public function ImageContainer( image:* = null )
		{
			super( );
			if ( image )
				this.addImage( image );
		}

		
		/**
		 * Adds a Bitmap at a specific depth to the ImageContainer
		 * @param String, Class, BitmapData or Bitmap that represents the image
		 * @param Integer that indicates the depth of the Bitmap
		 * 
		 */
		public function addImageAt( image:*, index:int ):void
		{
			this._index = index;
			this.addImage( image );
		}

		
		/**
		 * Adds the Bitmap to the container
		 * @param String, Class, BitmapData or Bitmap that represent the image
		 * 	
		 */
		public function addImage( image:* ):void
		{
			// Dispose previous image
			if ( this._image )
			{
				this.removeChild( this._image );
				this._image.bitmapData.dispose( );
			}
			
			// If the parameter is a String
			if ( image is String )
			{
				this._loader = loadSingle( image, this.onImage ) as ImageLoader;
				return;
			}

			// If the parameter is a Class definition
			if ( image is Class )
				this._image = new Bitmap( new image( 0, 0 ), PixelSnapping.AUTO, true );
			
			// If it's BitmapData
			else if ( image is BitmapData )
				this._image = new Bitmap( BitmapData( image ), PixelSnapping.AUTO, true );
			
			// If it's Bitmap	
			else if ( image is Bitmap )
				this._image = image;
				
			this.addChildAt( this._image, this._index );
		}

		
		/**
		 * Executes when the ImageLoader responds to the load call
		 * @param Event which triggers the image
		 * 
		 */
		private function onImage( event:LoadEvent ):void
		{
			// If the load is complete
			if ( event.type == LoadEvent.COMPLETE )
			{
				this._loader.removeEventListener( LoadEvent.COMPLETE, this.onImage );
				this.addChildAt( this._image = Bitmap( event.asset ), this._index );
				this.resize( );	
			}			
			
			// Dispatch the event	
			var myEvent:LoadEvent = event.clone( ) as LoadEvent;
			this.dispatchEvent( myEvent );
		}

		
		/**
		 * Resizes the ImageContainer when the width or height is set
		 * 
		 */
		private function resize():void
		{
			// Execute when the Image is loaded
			if ( this.isLoaded )
			{
				// Scale the image when the resizeMode is set to scale
				if ( this._resizeMode == MODE_SCALE )
				{
					this._image.width = this.width;
					this._image.height = this.height;
				}
				
				// Otherwise crop the image
				else
					this._image.scrollRect = new Rectangle( 0, 0, this.width, this.height );
			}			
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function set width( value:Number ):void
		{
			this._scaleX = 1.0;  
			this._width = value;
			
			this.resize( );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function get width():Number
		{
			var myWidth:Number = 0.0;
			if ( this.isLoaded )
				myWidth = !isNaN( this._width ) ? this._width : this._image.width;
			
			return myWidth * this._scaleX;	
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function set height( value:Number ):void
		{
			this._scaleY = 1.0;
			this._height = value;
			
			this.resize( );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function get height():Number
		{
			var myHeight:Number = 0.0;
			if ( this.isLoaded )
				myHeight = !isNaN( this._height ) ? this._height : this._image.height;

			return myHeight * this._scaleY;	
		}

		
		/**
		 * @inheritDoc
		 * 
		 */				
		override public function set scaleX( value:Number ):void
		{
			this._scaleX = value;
			this._width = NaN;
			
			this.resize( );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function get scaleX():Number
		{
			return this._scaleX;
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function set scaleY( value:Number ):void
		{
			this._scaleY = value;
			this._height = NaN;
			
			this.resize( );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */		
		override public function get scaleY():Number
		{
			return this._scaleY;
		}

		
		/**
		 * Reference to the Bitmap added to the container
		 * 
		 */		
		public function get image():Bitmap
		{
			return this._image;
		}

		
		/**
		 * Boolean that indicates that the image is loaded
		 * 
		 */
		public function get isLoaded():Boolean
		{
			return this._image != null;
		}

		
		/**
		 * String that indicates the resize mode that is used by the ImageContainer 
		 * 
		 */		
		public function set resizeMode( value:String ):void
		{
			this._resizeMode = value;
			this.resize( );
		}

		
		/**
		 * String that indicates the resize mode that is used by the ImageContainer 
		 * 
		 */		
		public function get resizeMode():String
		{
			return this._resizeMode;
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		override public function dispose( event:Event = null ):void
		{
			super.dispose( event );
			
			if ( this.isLoaded && this._loader )
				this._image.bitmapData.dispose( );
			
			this._image = null;
			this._loader = null;
		}
	}
}
